<template>
	<view>
		<nav-bar-sub></nav-bar-sub>
		<!-- 用于提示是否确认退出考试 -->
		<!-- <u-modal v-model="show" :content="content" :show-cancel-button="true" @confirm="handleConfirm"></u-modal> -->
		<!-- 渲染题目 -->
		<Answer :isReviewed='isReview' examType="exam" @submit='handleCommit' @timeEnd='handleConfirm'
			@onChange='onChange'></Answer>
	</view>

</template>

<script>
	import Answer from '@/components/dyw-answer/answer.vue';
	export default {
		data() {
			return {
				// show: false,
				// content: '是否确认交卷',
				isReview: true,
				confirmSubmitContent: '',
				questionList: [],
				examType: '', //考试类型
			}
		},
		onLoad(option) {
			var that = this
			this.$api.checkGrade(option.recordId)
				.then(res => {
					console.log(res.values);
					that.$store.state.examDetail = res.values
					var quesModules = res.values.modules
					// 提取出所有的题目
					for (let i = 0; i < quesModules.length; i++) {
						for (let j = 0; j < quesModules[i].questions.length; j++) {
							// 解决theoryExamAnswer可能为null的问题，把它赋值为字符串
							if (quesModules[i].questions[j].theoryExamAnswer == null) {
								quesModules[i].questions[j].unAnswered = true //标记此题未答
							}
							quesModules[i].questions[j].template = quesModules[i].template //防止template初始值是null
							quesModules[i].questions[j].tempAnswer = ''
							quesModules[i].questions[j].rightAnswer = ''
							// quesModules[i].questions[j].recordId = that.$store.state.examDetail.recordId //搬移recordId的位置
							// 遍历选项得出正确答案和用户答案
							if (quesModules[i].questions[j].template == 1 || quesModules[i].questions[j].template ==
								2) {
								// 单选题与多选题
								if (quesModules[i].questions[j].unAnswered) {
									quesModules[i].questions[j].theoryExamAnswer = {
										optAnswer: "未作答"
									}
								}

								// 用户答案
								quesModules[i].questions[j].tempAnswer = quesModules[i].questions[j]
									.theoryExamAnswer.optAnswer

								// 赋值选择题正确答案
								for (let k = 0; k < quesModules[i].questions[j].options.length; k++) {
									// 获取选择题的正确答案
									if (quesModules[i].questions[j].options[k].optAnswer == 1) {
										if (quesModules[i].questions[j].rightAnswer == '') {
											quesModules[i].questions[j].rightAnswer = quesModules[i].questions[j]
												.options[k].optLetter
										} else {
											quesModules[i].questions[j].rightAnswer += ',' + quesModules[i]
												.questions[j].options[k].optLetter
										}
									}
								}

								// 判断题目错选了哪一项
								var rightArr = quesModules[i].questions[j].rightAnswer.split(',')
								var userArr = quesModules[i].questions[j].tempAnswer.split(',')
								quesModules[i].questions[j].wrongArr = [] // 保存选中的错误的选项

								for (var item of userArr) {
									// 用户的该答案不在正确答案数组在
									if (rightArr.indexOf(item) < 0) {
										quesModules[i].questions[j].wrongArr.push(item)
									}
								}

							} else if (quesModules[i].questions[j].template == 3) {
								// 3.判断题
								quesModules[i].questions[j].wrongArr = ''
								if (quesModules[i].questions[j].unAnswered) {
									quesModules[i].questions[j].theoryExamAnswer = {
										optAnswer: "未作答"
									}
								}
								// 题目已经作答 获取用户判断题答案 0错误 1正确
								quesModules[i].questions[j].tempAnswer = quesModules[i].questions[j]
									.theoryExamAnswer.optAnswer

								// 为判断题的答案赋值
								if (quesModules[i].questions[j].options[0].optAnswer == 0) {
									quesModules[i].questions[j].rightAnswer = "错误"
								} else {
									quesModules[i].questions[j].rightAnswer = "正确"
								}
								
								// 判断题目错选了哪一项
								if (quesModules[i].questions[j].tempAnswer != quesModules[i].questions[j].options[0]
									.optAnswer) {
									quesModules[i].questions[j].wrongArr = quesModules[i].questions[j].tempAnswer
								}

							} else if (quesModules[i].questions[j].template == 4) {
								// 4.填空题
								if (!quesModules[i].questions[j].unAnswered) {
									// 题目已经作答 获取用户填空题答案
									quesModules[i].questions[j].blankArr = quesModules[i].questions[j]
										.theoryExamAnswer
										.shortAnswer.split("||~||")
										quesModules[i].questions[j].blankString = quesModules[i].questions[j].blankArr.join(',')
								} else {
									// 题目未作答
									quesModules[i].questions[j].blankArr = [] //新建blankArr属性，临时保存填空题答案
									quesModules[i].questions[j].blankString = '' //新建blankArr属性，临时保存填空题答案
									quesModules[i].questions[j].theoryExamAnswer = {}
									for (let m = 0; m < quesModules[i].questions[j].options.length; m++) {
										quesModules[i].questions[j].theoryExamAnswer.shortAnswer += "暂未作答||~||"
									}
								}

								// 为填空题附上正确答案
								for (let k = 0; k < quesModules[i].questions[j].options.length; k++) {
									// 把null赋值为空字符串
									if (quesModules[i].questions[j].rightAnswer == '') {
										quesModules[i].questions[j].rightAnswer = quesModules[i].questions[j]
											.options[
												k].optContent
									} else {
										quesModules[i].questions[j].rightAnswer += ',' + quesModules[i].questions[
												j]
											.options[k].optContent
									}
								}
							} else {
								// 5.简答题
								if (!quesModules[i].questions[j].unAnswered) {
									// 题目已经作答 获取用户的简答题答案
									quesModules[i].questions[j].tempAnswer = quesModules[i].questions[j]
										.theoryExamAnswer
										.shortAnswer
								} else {
									quesModules[i].questions[j].theoryExamAnswer = {}
									quesModules[i].questions[j].theoryExamAnswer.shortAnswer = "暂未作答"
								}

								// 正确答案
								quesModules[i].questions[j].rightAnswer = quesModules[i].questions[j].queAnswer
							}
							that.questionList.push(quesModules[i].questions[j])
						}
					}
					that.$store.state.questionList = that.questionList
					console.log('questionList:', that.questionList);
				})
			console.log('考题列表:', this.questionList);
		},
		components: {
			Answer
		},
		methods: {
			/* 提交答案 显示再次确认*/
			handleCommit(questionList) {
				// 显示是否确认交卷
				this.show = true;
			},

			// 已确认进行答案提交
			handleConfirm() {
				// uni.setStorageSync('questionList', questionList)
				// 进行交卷的api请求
				this.$api.commitExam(this.$store.state.examDetail.exam.id, this.$store.state.examDetail.recordId)
					.then(res => {
						console.log('交卷:', res);
					})

				uni.redirectTo({
					url: '/pages/exam/examResultPage',
				})
			},
			/* 题目答案变化
			 */
			onChange(answer) {
				console.log(answer)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}
</style>
